import React, { PureComponent } from 'react';
import dynamic from 'umi/dynamic';
import { connect } from 'dva';
import { Card, Form, } from 'antd';
import StandardTable from '@/components/StandardTable';
import StandardTableForm from '@/components/StandardTable/StandardTableForm';
import { profileStep3 as map } from '@/commons/map';
import { profileStep3 as columns } from '@/commons/columns';

const getValue = obj =>
  Object.keys(obj)
  .map(key => obj[key])
  .join(',');

/* eslint react/no-multi-comp:0 */
@connect(({ goodsUsers, loading }) => ({
  goodsUsers,
  loading: loading.models.goodsUsers,
}))
@Form.create()
class TableList extends PureComponent {
  state = {
    expandForm: false,
    formValues: {},
  };

  columns = [...columns];

  componentDidMount() {
    const { dispatch } = this.props;
    dispatch({
      type: 'goodsUsers/fetch',
    });
  }

  handleStandardTableChange = (pagination, filtersArg, sorter) => {
    const { dispatch } = this.props;
    const { formValues } = this.state;

    const filters = Object.keys(filtersArg).reduce((obj, key) => {
      const newObj = { ...obj };
      newObj[key] = getValue(filtersArg[key]);
      return newObj;
    }, {});

    const params = {
      from: pagination.current,
      size: pagination.pageSize,
      ...formValues,
      ...filters,
    };
    if (sorter.field) {
      params.sorter = `${sorter.field}_${sorter.order}`;
    }

    dispatch({
      type: 'goodsUsers/fetch',
      payload: params,
    });
  };

  handleFormReset = () => {
    const { form, dispatch } = this.props;
    form.resetFields();
    this.setState({
      formValues: {},
    });
    dispatch({
      type: 'goodsUsers/fetch',
      payload: {},
    });
  };

  toggleForm = () => {
    const { expandForm } = this.state;
    this.setState({
      expandForm: !expandForm,
    });
  };

  handleSearch = e => {
    e.preventDefault();

    const { dispatch, form } = this.props;

    form.validateFields((err, fieldsValue) => {
      if (err) return;

      const values = {
        ...fieldsValue,
        createdAt: fieldsValue.createdAt && fieldsValue.createdAt.valueOf(),
        updatedAt: fieldsValue.updatedAt && fieldsValue.updatedAt.valueOf(),
      };

      this.setState({
        formValues: values,
      });

      dispatch({
        type: 'goodsUsers/fetch',
        payload: values,
      });
    });
  };

  render() {
    const {
      goodsUsers: { data },
      loading,
      form,
    } = this.props;
    const { expandForm } = this.state;

    return (
      <Card bordered={false}>
        <StandardTableForm
          expandForm={expandForm}
          data={map}
          form={form}
          onSubmit={this.handleSearch}
          onReset={this.handleFormReset}
          onToggle={this.toggleForm}
        />
        <StandardTable
          loading={loading}
          data={data}
          bordered
          size="small"
          scroll={{ x: 1000 }}
          columns={this.columns}
          onChange={this.handleStandardTableChange}
          rowSelection={null}
        />
      </Card>
    );
  }
}

export default dynamic({
  loader: async () => () => <TableList />
});